{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Using \\*args arguments\n",
    "\n",
    "**\\*args** is specifically useful in functions where the number of input arguments are not fixed. There could be any number of arguments that could be given to the function. For example in this case **sum_of_numbers** function can have any number of integers to be summed up as an arguments. In such case, **\\*args** can be useful. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15\n"
     ]
    }
   ],
   "source": [
    "def sum_of_numbers(*args):\n",
    "    total = 0\n",
    "    for number in args:\n",
    "        total += number\n",
    "    return total\n",
    "\n",
    "print(sum_of_numbers(1,3,2,5,4))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Using \\*\\*kwargs arguments\n",
    "\n",
    "**\\*\\*kwargs** is used in the similar conditions where **\\*args** is used. The only difference between their use is that **\\*\\*kwargs** provides the functionality of keyword arguments i.e. along with the value, keyword is also provided. For example, for function **person_details**, there could any number of arguments can be given which also needs its keyword. \n",
    "In such situations **\\*\\*kwargs** is an important utility. Remember even the keywords are not fixed here.\n",
    "\n",
    "There are two ways by which **\\*\\*kwargs** based function can take input\n",
    "1. Using keyword arguments \n",
    "2. Using a dictionary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def person_details(**kwargs):\n",
    "    print(\"Personal Details\")\n",
    "    for key, value in kwargs.items():\n",
    "        print(\"{} : {}\".format(key,value))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Personal Details\n",
      "firstName : Jason\n",
      "lastName : Scott\n",
      "country : US\n"
     ]
    }
   ],
   "source": [
    "person_details(firstName = \"Jason\", lastName = \"Scott\", country = \"US\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Personal Details\n",
      "name : Ratan\n",
      "country : India\n",
      "age : 23\n"
     ]
    }
   ],
   "source": [
    "person_details(name = \"Ratan\", country = \"India\", age = 23)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Till now we have seen positional arguments as an input to functions with **\\*\\*kwargs**. Now Let's see how we can input a dictionary to the same. Operater **\\*\\*** unrolls the contents of the dictionary."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Personal Details\n",
      "name : Vikas Singh\n",
      "gender : Male\n"
     ]
    }
   ],
   "source": [
    "input_dict = {'name' : 'Vikas Singh' , 'gender' : 'Male'}\n",
    "person_details(**input_dict)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
